package com.fingard.dsp.bank.directbank.rccosd01;

import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqPayRecDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqQrySglBatPayDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author Parker on 2019/7/18.
 * @version 1.0
 */
public class RCCOSD01SinglePay extends RCCOSD01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RCCOSD01Header bankHeader = new RCCOSD01Header();
        bankHeader.serialNo = p_req1908.reqHeader.transSeqID;
        if("1".equals(p_req1908.singleDetail.sameBnk)){
            bankHeader.bsnCode = "QRCB003";
        }else {
            bankHeader.bsnCode = "QRCB004";
        }
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        try{
            writeCommonHeader(xmlWriter,bankHeader);
            XElement tmpXElement = new XElement("Body", "");
            tmpXElement.addChild(new XElement("PayerAcNo", p_req1908.singleDetail.payAct));
            tmpXElement.addChild(new XElement("PayeeAcNo", p_req1908.singleDetail.recAct));
            tmpXElement.addChild(new XElement("PayeeAcName", p_req1908.singleDetail.recName));
            tmpXElement.addChild(new XElement("Currency", StringHelper.hasAnyChar(p_req1908.singleDetail.payCur)? convertCurToBank(p_req1908.singleDetail.payCur): "01"));
            tmpXElement.addChild(new XElement("Amount", p_req1908.singleDetail.payAmount));
            if(!"1".equals(p_req1908.singleDetail.sameBnk)){
                tmpXElement.addChild(new XElement("UnionBankNo", p_req1908.singleDetail.cnapsCode));
                boolean isNXY = false;//判断是否走农信银

                if (p_req1908.singleDetail.cnapsName.contains("农信")) {
                    isNXY = true;
                }
                tmpXElement.addChild(new XElement("rmtType", isNXY ? "2" : p_req1908.singleDetail.paySpd));
            }
            tmpXElement.addChild(new XElement("Remark", p_req1908.singleDetail.purpose));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            //发送到银行
            String[] retStrArr = sendToBank(xmlWriter.toXmlString(),bankHeader);
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//返回码
                    String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//返回信息
                    if(tmpRetCode != null && "000000".equals(tmpRetCode)){
                        retDetail.respCode = "0";
                        retDetail.transState = tmpRetCode + "+" + tmpBkReader.getFirstText("/Message/Body/orderState");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.getFirstText("/Message/Body/orderFlowDescript");
                        retDetail.reqSeqID = p_req1908.reqHeader.reqSeqID;
                    }else{
                        retDetail.respCode = "-999";
                        retDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    retDetail.respCode = "-999";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-999";
                retDetail.respInfo = retStrArr[1];
            }
        }catch(Exception e){
            retDetail.respCode = "-999";
            retDetail.respInfo = "查询失败：" + e.getMessage();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }


    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RCCOSD01Header bankHeader = new RCCOSD01Header();
        bankHeader.serialNo = p_req1944.reqHeader.transSeqID;
        bankHeader.bsnCode = "QRCB005";
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        writeCommonHeader(xmlWriter,bankHeader);
        XElement tmpXElement = new XElement("Body", "");
        tmpXElement.addChild(new XElement("orderFlowNo", p_req1944.reqHeader.reqSeqID));
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();
        //发送到银行
        String[] retStrArr = sendToBank(xmlWriter.toXmlString(),bankHeader);
        if (retStrArr[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
            if(tmpBkReader != null){
                String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//返回码
                String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//返回信息
                if(tmpRetCode != null && "000000".equals(tmpRetCode)){
                    retDetail.respCode = "0";
                    retDetail.reqSeqID = p_req1944.reqHeader.reqSeqID;
                    retDetail.transState = tmpRetCode + "+" + tmpBkReader.getFirstText("/Message/Body/Proc_stat");
                    retDetail.payInfoCode = retDetail.transState;
                    retDetail.respInfo = tmpBkReader.getFirstText("/Message/Body/orderFlowDescript");
                }else{
                    retDetail.respCode = "-999";
                    retDetail.respInfo = tmpRetInfo;
                }
            } else {
                retDetail.respCode = "-999";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else {
            retDetail.respCode = "-999";
            retDetail.respInfo = retStrArr[1];
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        //返回ATS的对象
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1908BAT);
        retAts.reqBatchNO = p_req1908BAT.reqBatchNO;
        retAts.batRespCode = "0";
        retAts.batTransState = "";
        retAts.batPayInfoCode = "";
        while (p_req1908BAT.readRecord()) {
            ReqPayRecDetail tmpDetail = p_req1908BAT.readedDetail;
            ReqSingle req1908 = new ReqSingle(p_req1908BAT.reqHeader);
            req1908.singleDetail = tmpDetail;
            RetAtsSingle eachRet = d_1908_SglPay(req1908);

            retAts.writeRetDetail(eachRet.retDetail);
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);
        retAts.reqBatchNO = p_req1944BAT.reqBatchNO;
        try {
            retAts.batRespCode = "0";
            while (p_req1944BAT.readRecord()) {
                ReqQrySglBatPayDetail tmpDetail = p_req1944BAT.readedDetail;
                ReqQrySingle req1944 = new ReqQrySingle(tmpDetail, p_req1944BAT.reqHeader);
                RetAtsSingle resp1944 = d_1944_QrySglPay(req1944);
                retAts.writeRetDetail(resp1944.retDetail);
            }
        }catch (Exception ex) {
            WriteBankLogLn(ex);
            retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);
            retAts.batRespCode = "-1";
            retAts.batRespInfo = ex.getMessage();
        }

        retAts.writeAfterDetail();
        return retAts;
    }
}
